package com.yinzifan.swagger.config;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.ApiSelectorBuilder;
import springfox.documentation.spring.web.plugins.Docket;

@Configuration
@ComponentScan(basePackages="com.yinzifan.swagger")
public class SwaggerConfiguration {
	@Autowired
	private SwaggerInfo swaggerInfo;
	@Bean
	public Docket controllerApi() {
		ApiSelectorBuilder builder = new Docket(DocumentationType.SWAGGER_2)
		.groupName(swaggerInfo.getGroupName())
		.apiInfo(apiInfo())
		.select();
		if(!StringUtils.isEmpty(swaggerInfo.getBasePackage())) {
			builder.apis(RequestHandlerSelectors.basePackage(swaggerInfo.getBasePackage()));
		}
		if(!StringUtils.isEmpty(swaggerInfo.getAntPath())) {
			builder.paths(PathSelectors.ant(swaggerInfo.getAntPath()));
		}
		return builder.build();
	}

	private ApiInfo apiInfo() {
		return new ApiInfoBuilder()
				.title(swaggerInfo.getTitle())
				.description(swaggerInfo.getDescription())
				.build();
	}
	@Bean
	public Docket defaultApi() {
		return new Docket(DocumentationType.SWAGGER_2)
				.select().apis(RequestHandlerSelectors.basePackage(BasicErrorController.class.getPackage().getName()))
				.build();
	}
	
}
